package com.powernode.controller;

import com.github.pagehelper.Page;
import com.powernode.common.Constants;
import com.powernode.common.Result;
import com.powernode.domain.Customer;
import com.powernode.domain.User;
import com.powernode.dto.CustomerDto;
import com.powernode.service.CustomerService;
import com.powernode.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 描述：处理客户管理的所有功能
 */
@RestController
@RequestMapping("customer")
public class CustomerController {

    @Resource
    private UserService userService;

    @Resource
    private CustomerService customerService;

    //分页查询
    @GetMapping("query.action")
    public Result query(CustomerDto customerDto,HttpSession session) {
        //判断当前用户是否为管理员
        User user = (User) session.getAttribute(Constants.SESSION_USER_KEY);
        if(user.getRole().equals(Constants.USER_TYPE_NORMAL)){
            customerDto.setUserId(user.getId());
        }
        Page<Customer> page = customerService.queryForPage(customerDto);
        return new Result(0,"查询成功",page.getTotal(),page.getResult());
    }

    //添加
    @PostMapping("add.action")
    public Result add(Customer customer, HttpSession session) {
        //设置在职状态
        customer.setDeleted((Constants.USER_DEFAULT_1));
        User user = (User)session.getAttribute(Constants.SESSION_USER_KEY);
        //设置头像
        customer.setUserId(user.getId());
        //设置创建时间
        customer.setCreateTime(new Date());
        //保存
        try {
            customerService.insertSelective(customer);
            return new Result(200,"添加成功");
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(-1,"添加失败");
        }
    }

    //更新
    @PostMapping("update.action")
    public Result update(Customer customer) {
        try {
            customerService.updateByPrimaryKeySelective(customer);
            return new Result(200,"修改成功");
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(-1,"修改失败");
        }
    }

    //删除
    @PostMapping("del.action")
    public Result del(Integer id) {
        Customer customer = new Customer();
        customer.setId(id);
        customer.setDeleted(Constants.USER_DEFAULT_2);
        customer.setDeleteTime(new Date());
        try {
            customerService.updateByPrimaryKeySelective(customer);
            return new Result(200,"删除成功");
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(-1,"删除失败");
        }
    }
    /**
     * 修改客户所属的业务员
     */
    @PostMapping("changeCustomerUser.action")
    public Result changeCustomerUser(Integer userId,Integer[] customerIds) {
        if(userId == null){
            return new Result(-1,"请选择业务员");
        }
        if(customerIds == null || customerIds.length == 0){
            return new Result(-1,"请选择要转移的客户");
        }
        try {
            customerService.changeCustomerUser(userId,customerIds);
            return new Result(200,"转移成功");
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(-1,"转移失败");
        }
    }


}
